package com.drinks.yiniang.common.recycler.decoration;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.hansen.library.utils.ScreenSizeUtils;

/**
 * Created by han on 2019/3/19 0019.
 */

public class CHotStaggeredItemDecoration extends RecyclerView.ItemDecoration {
    private int mFirstStaggeredPosition = -1;
    private int mSpace;

    public CHotStaggeredItemDecoration(Context context, int space) {
        this.mSpace = ScreenSizeUtils.dp2px(context, space);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();

        //通过layoutposition设置会错乱，改成StaggeredGridLayoutManager.layoutParams.getSpanIndex()
        // 中间间隔
        if (layoutParams.getSpanIndex() == 0) {
            outRect.left = mSpace;
            outRect.right = mSpace / 2;
        } else {
            // item为奇数位，设置间隔
            outRect.left = mSpace / 2;
            outRect.right = mSpace;
        }

        // 上方间隔
        outRect.top = mSpace;
        outRect.bottom = 0;
    }
}
